package com.bye.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bye.dto.CustomerDto;
import com.bye.dto.ProductCategoryDto;
import com.bye.service.ProductCategoryService;
import com.bye.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    //展示商品类别
    @RequestMapping("/showProductCategory")
    public Result showCustomer(@RequestBody ProductCategoryDto productCategoryDto) {
        IPage<ProductCategoryDto> customerList = productCategoryService.showProductCategory(productCategoryDto);
        List<ProductCategoryDto> list = customerList.getRecords();
        long total = customerList.getTotal();
        return Result.success().put("productCategoryList",list).put("total",total);
    }

    //删除类别
    @RequestMapping("/deleteProductCategory")
    public Result deleteProductCategory(@RequestBody ProductCategoryDto productCategoryDto) {
        int i = productCategoryService.deleteProductCategory(productCategoryDto);
        if(i>0){
            return Result.success("删除成功");
        }else {
            return Result.success("删除失败");
        }
    }

    //更改类别的状态
    @RequestMapping("changeEnabled")
    public Result changeEnabled(@RequestBody ProductCategoryDto productCategoryDto) {
        int i = productCategoryService.changeEnabled(productCategoryDto);
        if(i>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    //用于回显
    @RequestMapping("/selectCategory")
    public Result selectCategory(@RequestBody ProductCategoryDto productCategoryDto) {
        ProductCategoryDto list = productCategoryService.selectCategory(productCategoryDto);
        return Result.success().put("productCategory",list);
    }

    //修改
    @RequestMapping("/changeCategory")
    public Result changeCategory(@RequestBody ProductCategoryDto productCategoryDto) {
        System.out.println("嘻嘻嘻嘻嘻");
        int i = productCategoryService.changeCategory(productCategoryDto);
        if (i > 0){
            return Result.success("修改成功");
        }else {
            return Result.success("修改失败");
        }
    }
}
